﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ewk.Runtime.Serialization
{
    public abstract class DatafieldSerializer<TDatafieldAttribute> where TDatafieldAttribute : DatafieldAttribute
    {
        /// <summary>
        /// Gets the <see cref="Type"/> of the field to be serialized or deserialized.
        /// </summary>
        public abstract Type DatafieldType { get; }

        public abstract string Serialize(object datafield, TDatafieldAttribute datafieldAttribute);

        public abstract object Deserialize(string datafield, TDatafieldAttribute datafieldAttribute);

        /// <summary>
        /// Occurs when subscribed on and the <see cref="DatafieldSerializer{"/> cannot
        /// serialize or deserialize the field.
        /// </summary>
        /// <remarks>
        /// When a client has not subscribed on this event and the situation of Failed occurs,
        /// a <see cref="DatafieldSerializationException"/> is trown.
        /// </remarks>
        public event EventHandler<DatafieldSerializationFailedEventArgs> DatafieldSerializationFailed;

        protected void OnDatafieldSerializationFailed(string message)
        {
            if (DatafieldSerializationFailed == null)
            {
                throw new DatafieldSerializationException(message);
            }

            DatafieldSerializationFailed(this, new DatafieldSerializationFailedEventArgs(message));
        }
    }
}
